package menu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;

import utility.XMLConfigurator;

public class Settings {
	public String 			playerName;
	public int 				playerTot;
	public int 				playerColor;
	public int				playerScreen;
	public int				playerMusic;
	public int				playerEffects;
	private XMLConfigurator	xml;
	private File			f = null;
	
	public Settings()
	{
		xml = new XMLConfigurator();
		f = new File("src/menu/config.xml");
		if (f.isFile())
			getParameters();
		else
			newXML();
	}
	
	private void getParameters()
	{
		System.out.println("GETTING PARAMETERS");
		
		try {
			xml.openXML(f.getAbsolutePath());
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		playerName = xml.getElement("LASTPLAYER").getAttributeValue("name");
		System.out.println("PLAYERNAME : " + playerName);
		Element e = xml.findChild(playerName);
		System.out.println("ELEMENT : " + e.getText());
		
		playerTot     = 	Integer.parseInt(e.getChild("number").getText());
		playerColor   = 	Integer.parseInt(e.getChild("color").getText());
		playerScreen  = 	Integer.parseInt(e.getChild("screen").getText());
		playerMusic   = 	Integer.parseInt(e.getChild("music").getText());
		playerEffects = 	Integer.parseInt(e.getChild("effects").getText());
	}
	
	private void newXML()
	{
		Element subValue = null;
		Element lastPlayer = null;
		
		xml.createDocument("TRACEON");
		
		lastPlayer = new Element("LASTPLAYER");
		lastPlayer.setAttribute("name", "DEFAULT");
		xml.addRootChild(lastPlayer);
		
		Element player = new Element("PLAYER");
		player.setAttribute("name", "DEFAULT");
		
		subValue = new Element("number");
		subValue.setText("3");
		player.addContent(subValue);
		
		subValue = new Element("color");
		subValue.setText("3");
		player.addContent(subValue);
		
		subValue = new Element("screen");
		subValue.setText("0");
		player.addContent(subValue);
		
		subValue = new Element("music");
		subValue.setText("0");
		player.addContent(subValue);
		
		subValue = new Element("effects");
		subValue.setText("0");
		player.addContent(subValue);
		
		xml.addRootChild(player);
		
		saveXML();
	}
	
	public void newPlayer()
	{
		System.out.println("NEW PLAYER");
		Element subValue = null;
		Element lastPlayer = null;
		
		Element player = new Element("PLAYER");
		player.setAttribute("name", playerName);
		
		subValue = new Element("number");
		subValue.setText(Integer.toString(playerTot));
		player.addContent(subValue);
		
		subValue = new Element("color");
		subValue.setText(Integer.toString(playerColor));
		player.addContent(subValue);
		
		subValue = new Element("screen");
		subValue.setText(Integer.toString(playerScreen));
		player.addContent(subValue);
		
		subValue = new Element("music");
		subValue.setText(Integer.toString(playerMusic));
		player.addContent(subValue);
		
		subValue = new Element("effects");
		subValue.setText(Integer.toString(playerEffects));
		player.addContent(subValue);
		
		xml.addRootChild(player);
		System.out.println("TOT CHILDREN: " + xml.getChildrenNumber());
		
		lastPlayer = xml.getRoot().getChild("LASTPLAYER");
		lastPlayer.setAttribute("name", playerName);
		
		saveXML();
	}
	
	public void updPlayer()
	{
		Element subValue = null;
		Element lastPlayer = null;
		
		Element player = xml.findChild(playerName);
		
		subValue = player.getChild("number");
		subValue.setText(Integer.toString(playerTot));
		
		subValue = player.getChild("color");
		subValue.setText(Integer.toString(playerColor));
		
		subValue = player.getChild("screen");
		subValue.setText(Integer.toString(playerScreen));
		
		subValue = player.getChild("music");
		subValue.setText(Integer.toString(playerMusic));
		
		subValue = player.getChild("effects");
		subValue.setText(Integer.toString(playerEffects));
		
		lastPlayer = xml.getRoot().getChild("LASTPLAYER");
		lastPlayer.setAttribute("name", playerName);
		
		saveXML();
	}
	
	private void saveXML()
	{
		try {
			xml.writeXML(f.getAbsolutePath());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void updateXML()
	{
		Element e = null;
		e = xml.findChild(playerName);
		System.out.println("ISNULL? " + e==null);
		if(e==null)
		{
			System.out.println("CREATING NEW PLAYER " + playerName);
			newPlayer();
		}
		else
		{
			System.out.println("UPDATE PLAYER " + playerName);
			updPlayer();
		}
		saveXML();
	}
	
	@SuppressWarnings("unchecked")
	public String[] getPlayers()
	{
		String[] players;
		int i = 0;
		List<Element> elements = xml.getRoot().getChildren();
		System.out.println("TOTAL CHILDREN " +  elements.size());
		Iterator<Element> it = elements.iterator();
		players = new String[elements.size()-1];
		
		while (it.hasNext())
		{
			Element e = it.next();
			System.out.println("GETNAME " + e.getName() + "(" + i + ")");
			if(e.getName().equals("PLAYER"))
			{
				System.out.println("FOUND PLAYER " + e.getAttributeValue("name"));
				players[i] = e.getAttributeValue("name");
				i++;
			}
		}
		return players;
	}
	
	public void getPlayer(String name)
	{
		Element e =  xml.findChild(name);
		playerTot     = 	Integer.parseInt(e.getChild("number").getText());
		playerColor   = 	Integer.parseInt(e.getChild("color").getText());
		playerScreen  = 	Integer.parseInt(e.getChild("screen").getText());
		playerMusic   = 	Integer.parseInt(e.getChild("music").getText());
		playerEffects = 	Integer.parseInt(e.getChild("effects").getText());
	}
}
